//
//  LGPatientSportsSendController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/8/9.
//  运动方案列表

#import "LGPatientSportsSendController.h"
//调整运动方案
#import "LGPatientSportAdjustController.h"
#import "LGPatientSportsSendCell.h"
@interface LGPatientSportsSendController ()<UITableViewDelegate,UITableViewDataSource>
/**   */
@property (nonatomic , strong)UILabel *patientLabel;
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)UIView *lineView;
/**   */
@property (nonatomic , strong)UIButton *adjustBtn;
/**   */
@property (nonatomic , strong)UIButton *sendButton;
/**   */
@property (nonatomic , strong)NSMutableArray *dataArray;
/**   */
@property (nonatomic , copy)NSString *pageNo;
/**   */
@property (nonatomic , copy)NSString *planid;

@end

@implementation LGPatientSportsSendController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.planid = @"";
    self.title = @"选择运动方案";
    self.view.backgroundColor = [UIColor colorWithString:@"#FFFFFF"];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.lineView];
    [self.view addSubview:self.adjustBtn];
    [self.view addSubview:self.sendButton];
    [self emptyViewWithSuperView:self.view frame:self.view.frame];
    [self requestData];
    [self requestPatientData];
    
}

-(void)requestPatientData{
    [RequestUtil withPOST:@"/doctor/patient.health_records/view" parameters:@{@"patient_id":self.patientid} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            self.patientLabel.text = [NSString stringWithFormat:@"%@  %@  %@",returnData[@"realname"],returnData[@"gender"],returnData[@"age"]];
        }
    } error:^(NSError *error) {
            
    }];
}

-(void)requestData{
    self.pageNo = @"1";
    self.dataArray = [NSMutableArray array];
    [self requestMoreData];
}

-(void)requestMoreData{
    [RequestUtil withPOST:@"/doctor/sport.plan/pagedList" parameters:@{@"pageNo":self.pageNo,@"pageSize":@"20"} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if(code == 200){
            NSArray *tempArray = returnData[@"data"];
            if(tempArray.count>0){
                self.pageNo = [NSString stringWithFormat:@"%ld",[returnData[@"current_page"] integerValue]+1];
                [self.dataArray addObjectsFromArray:tempArray];
            }else{
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }
        [self.tableView reloadData];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无运动方案" offset:0];
        }
    } error:^(NSError *error) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无运动方案" offset:0];
        }
    }];
}

//调整后发送
-(void)adjustPlanAction{
    if(self.planid.length == 0){
        [TooltipView showMessage:@"请选择运动方案" offset:0];
        return;
    }
    LGPatientSportAdjustController *controller = [[LGPatientSportAdjustController alloc]init];
    controller.patientid = self.patientid;
    controller.planid = self.planid;
    [self.navigationController pushViewController:controller animated:YES];
}

//直接发送
-(void)sendPlanAction{
    if(self.planid.length == 0){
        [TooltipView showMessage:@"请选择运动方案" offset:0];
        return;
    }
    [RequestUtil withPOST:@"/doctor/sport.plan/send" parameters:@{@"plan_id":self.planid,@"patient_ids":self.patientid} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            [TooltipView showMessage:@"发放成功" offset:0];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [TooltipView showMessage:message offset:0];
        }
    } error:^(NSError *error) {
        [TooltipView showMessage:@"发送失败，请重试" offset:0];
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGPatientSportsSendCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *tempDic = self.dataArray[indexPath.row];
    cell.dataDic = tempDic;
    cell.selectBtn.selected = ([LGNSString(tempDic[@"id"]) isEqualToString:self.planid])?YES:NO;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.planid = LGNSString(self.dataArray[indexPath.row][@"id"]);
    [self.tableView reloadData];
}


#pragma mark
#pragma mark ====> 创建控件
-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight-viewPix(84)-bottomSafeBarHeight/4.0) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGPatientSportsSendCell class] forCellReuseIdentifier:@"cell"];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.rowHeight = viewPix(123);
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = [self tableHeaderView];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestData)];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(requestMoreData)];
    }
    return _tableView;
}

-(UIView *)lineView{
    if(!_lineView){
        _lineView = [[UIView alloc]initWithFrame:CGRectMake(0, kMaxY(self.tableView.frame), Screen_W, 0.5)];
        _lineView.backgroundColor = [UIColor colorWithString:@"#F2F2F2"];
    }
    return _lineView;
}

-(UIButton *)adjustBtn{
    if(!_adjustBtn){
        _adjustBtn = [UIButton buttonWithTitle:@"调整后发放" titleFont:LGFontWeight(16, UIFontWeightMedium) textColor:@"#429BFF" imageName:@""];
        _adjustBtn.frame = CGRectMake(viewPix(47), kMaxY(self.tableView.frame)+viewPix(12), (Screen_W-viewPix(118))/2.0, viewPix(46));
        _adjustBtn.layer.borderColor = [UIColor colorWithString:@"#429BFF"].CGColor;
        _adjustBtn.layer.borderWidth = 1;
        _adjustBtn.cornerRidus = 10;
        [_adjustBtn addTarget:self action:@selector(adjustPlanAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _adjustBtn;
}

-(UIButton *)sendButton{
    if(!_sendButton){
        _sendButton = [UIButton buttonWithTitle:@"直接发送" titleFont:LGFontWeight(16, UIFontWeightMedium) textColor:@"#FFFFFF" imageName:@""];
        _sendButton.frame = CGRectMake(kMaxX(self.adjustBtn.frame)+viewPix(24), kMaxY(self.tableView.frame)+viewPix(12), (Screen_W-viewPix(118))/2.0, viewPix(46));
        _sendButton.backgroundColor = [UIColor colorWithString:@"#429BFF"];
        _sendButton.cornerRidus = 10;
        [_sendButton addTarget:self action:@selector(sendPlanAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sendButton;
}

-(UILabel *)patientLabel{
    if(!_patientLabel){
        _patientLabel = [UILabel lableWithText:@"" colorString:@"#333333" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _patientLabel;
}

-(UIView *)tableHeaderView{
    UIView *baseView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(46))];
    baseView.backgroundColor = [UIColor colorWithString:@"#F9F9F9"];
    UILabel *titleLabel = [UILabel lableWithText:@"发放患者:" colorString:@"#666666" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
    [baseView addSubview:titleLabel];
    [baseView addSubview:self.patientLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(baseView).offset(viewPix(17));
        make.top.equalTo(baseView).offset(viewPix(16));
    }];
    [self.patientLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLabel.mas_right).offset(viewPix(12));
        make.centerY.equalTo(titleLabel);
    }];
    return baseView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
